package sea.simulate;

import javax.swing.JFileChooser;
import sea.util.PublicParam;
import sea.util.SimHyAHelper;
import sea.util.WinHelper;

/**
 *
 * This class builds the layout of Dynamix Switch interface
 * 
 * @author  Zhirun Ding
 * 
 * Created on 05/07/2012
 * 
 */

public class SimulateHDynswForm extends javax.swing.JDialog {
    
    /** Creates new form SimulateHDynswForm */
    public SimulateHDynswForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dynamicSwitchButtonGroup = new javax.swing.ButtonGroup();
        populationButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        noneRadioButton = new javax.swing.JRadioButton();
        populationRadioButton = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        d2cRadioButton = new javax.swing.JRadioButton();
        c2dRadioButton = new javax.swing.JRadioButton();
        rulesRadioButton = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        fileNameText = new javax.swing.JTextField();
        chooseButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sea.SimHyAGui.class).getContext().getResourceMap(SimulateHDynswForm.class);
        setTitle(resourceMap.getString("SimulateHybridForm.title")); // NOI18N
        setName("SimulateHybridForm"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        dynamicSwitchButtonGroup.add(noneRadioButton);
        noneRadioButton.setText(resourceMap.getString("noneRadioButton.text")); // NOI18N
        noneRadioButton.setToolTipText(resourceMap.getString("noneRadioButton.toolTipText")); // NOI18N
        noneRadioButton.setName("noneRadioButton"); // NOI18N

        dynamicSwitchButtonGroup.add(populationRadioButton);
        populationRadioButton.setText(resourceMap.getString("populationRadioButton.text")); // NOI18N
        populationRadioButton.setToolTipText(resourceMap.getString("populationRadioButton.toolTipText")); // NOI18N
        populationRadioButton.setName("populationRadioButton"); // NOI18N
        populationRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                populationRadioButtonStateChanged(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        populationButtonGroup.add(d2cRadioButton);
        d2cRadioButton.setText(resourceMap.getString("d2cRadioButton.text")); // NOI18N
        d2cRadioButton.setToolTipText(resourceMap.getString("d2cRadioButton.toolTipText")); // NOI18N
        d2cRadioButton.setEnabled(false);
        d2cRadioButton.setName("d2cRadioButton"); // NOI18N

        populationButtonGroup.add(c2dRadioButton);
        c2dRadioButton.setText(resourceMap.getString("c2dRadioButton.text")); // NOI18N
        c2dRadioButton.setToolTipText(resourceMap.getString("c2dRadioButton.toolTipText")); // NOI18N
        c2dRadioButton.setEnabled(false);
        c2dRadioButton.setName("c2dRadioButton"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(c2dRadioButton)
                    .addComponent(d2cRadioButton))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(d2cRadioButton)
                .addGap(18, 18, 18)
                .addComponent(c2dRadioButton)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        dynamicSwitchButtonGroup.add(rulesRadioButton);
        rulesRadioButton.setText(resourceMap.getString("rulesRadioButton.text")); // NOI18N
        rulesRadioButton.setToolTipText(resourceMap.getString("rulesRadioButton.toolTipText")); // NOI18N
        rulesRadioButton.setName("rulesRadioButton"); // NOI18N
        rulesRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rulesRadioButtonStateChanged(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        fileNameText.setText(resourceMap.getString("fileNameText.text")); // NOI18N
        fileNameText.setEnabled(false);
        fileNameText.setName("fileNameText"); // NOI18N

        chooseButton.setEnabled(false);
        chooseButton.setLabel(resourceMap.getString("chooseButton.label")); // NOI18N
        chooseButton.setName("chooseButton"); // NOI18N
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        backButton.setText(resourceMap.getString("backButton.text")); // NOI18N
        backButton.setName("backButton"); // NOI18N
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        helpButton.setText(resourceMap.getString("helpButton.text")); // NOI18N
        helpButton.setName("helpButton"); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jLabel1)
                .addContainerGap(404, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(109, 109, 109)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rulesRadioButton)
                    .addComponent(noneRadioButton)
                    .addComponent(populationRadioButton))
                .addContainerGap(266, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chooseButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(helpButton)
                .addGap(19, 19, 19))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fileNameText))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(noneRadioButton)
                        .addGap(18, 18, 18)
                        .addComponent(populationRadioButton)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(rulesRadioButton)
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(fileNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(370, 370, 370)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(chooseButton)
                            .addComponent(backButton)
                            .addComponent(helpButton))))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        //generate simulate command
        toSimulateCommand();
        
        //close this form
        this.dispose();
}//GEN-LAST:event_backButtonActionPerformed

    void toSimulateCommand() {
        String s = "";
        //Dynamic Switch
        if (noneRadioButton.isSelected()) {
            //none
            s = ",switch.none";            
        } else if (populationRadioButton.isSelected()) {
            //population
            if (d2cRadioButton.isSelected()) {
                s = ",switch.population=d2c";
            } else if (c2dRadioButton.isSelected()) {
                s = ",switch.population=c2d";
            }            
        } else if (rulesRadioButton.isSelected()) {
            //rules
            if (fileNameText.getText().length() > 0) {
                s = ",switch.rules=" + fileNameText.getText();
            }
        }
        
        PublicParam.TempSimHyACommandExpression2 = s;
    }
    
    private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
        //show dialog for setting working direcotry
        JFileChooser c = new JFileChooser();
        c.setDialogTitle("Specifies a file containing global switching rules for transitions in hybrid simulation");
                       
        int result = c.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            //rule file
            String path = c.getSelectedFile().getAbsolutePath();            
            fileNameText.setText(path);
        }
    }//GEN-LAST:event_chooseButtonActionPerformed

    private void populationRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_populationRadioButtonStateChanged
        d2cRadioButton.setEnabled(populationRadioButton.isSelected());
        c2dRadioButton.setEnabled(populationRadioButton.isSelected());                
    }//GEN-LAST:event_populationRadioButtonStateChanged

    private void rulesRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rulesRadioButtonStateChanged
        fileNameText.setEnabled(rulesRadioButton.isSelected());
        chooseButton.setEnabled(rulesRadioButton.isSelected());
    }//GEN-LAST:event_rulesRadioButtonStateChanged

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        WinHelper.showHelp("simulate/dynamicswitch");
    }//GEN-LAST:event_helpButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SimulateHDynswForm dialog = new SimulateHDynswForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JRadioButton c2dRadioButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JRadioButton d2cRadioButton;
    private javax.swing.ButtonGroup dynamicSwitchButtonGroup;
    private javax.swing.JTextField fileNameText;
    private javax.swing.JButton helpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton noneRadioButton;
    private javax.swing.ButtonGroup populationButtonGroup;
    private javax.swing.JRadioButton populationRadioButton;
    private javax.swing.JRadioButton rulesRadioButton;
    // End of variables declaration//GEN-END:variables
    
}
